package tree.迭代bfs;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author songZiHao
 * @version 1.0.0
 * @ClassName 多叉树的层序遍历429.java
 * @Description https://leetcode-cn.com/problems/n-ary-tree-level-order-traversal/
 * @createTime 2021年04月25日 22:36:00
 */
public class 多叉树的层序遍历429 {
    class Node {
        public int val;
        public List<Node> children;

        public Node() {}

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    };

    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> lists=new ArrayList<>();
        if (root == null) {
            return lists;
        }
        Queue<Node> queue=new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()){
            int size = queue.size();
            List<Integer> list=new ArrayList<>(size);
            for (int i = 0; i < size; i++) {
                Node poll = queue.poll();
                list.add(poll.val);
                queue.addAll(poll.children);
            }
            lists.add(list);
        }
        return lists;
    }



}
